const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
// zh_jdgjb/pages/index/index_c.js
var app = getApp();

var util = require("../../utils/util.js");
var base = require('./../../../config.js');
var Moment = require("./../../utils/moment.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectList: [],
    hotCity: [],
    strategy: [],
    adImg: '',
    imgUrl: '',
    url1: 'https://tzlx.tiaze.com/attachment/',
    show: 'show',
    day1: wx2my.getStorageSync(['day1']).data,
    day2: wx2my.getStorageSync(['day2']).data,
    tags: [{
      name: '北京',
      id: 0
    }, {
      name: '上海',
      id: 1
    }, {
      name: '成都',
      id: 2
    }, {
      name: '重庆',
      id: 3
    }, {
      name: '杭州',
      id: 4
    }, {
      name: '西安',
      id: 5
    }, {
      name: '南京',
      id: 6
    }, {
      name: '武汉',
      id: 7
    }, {
      name: '厦门',
      id: 8
    }, {
      name: '全部',
      id: 9
    }],
    // 芝麻信用
    canUseCreditAssessment: my.canIUse('zm-credit-assessment'), //判断组件是否可用
    query: {
      serviceId:'2019111100000000000003791600', // serviceId为商户服务ID，芝麻商户平台可查到
      categoryId:'CREDIT_HOTEL_ONLINE_ALL', // categoryId为服务类目，芝麻商户平台可查到
    },
  },
  // 组件内部错误，非必需属性，建议写上用来监听组件内部错误
  onError(event) {
    const { detail = {} } = event
    const { error = {} } = detail
    console.log({ error })
    // todo
  },
  // 轻会员插件
  gotoMember() {
    let that = this;
    console.log('轻会员点击');
    app.getUserInfo(function(g) {
      console.log('logload.....')
      // console.log(g);
      // wx2my.setStorageSync("userInfo", g.data)
      // t.setData({
      //   user_info: g.data
      // })
      // console.log(g.data);
      // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
      my.navigateToMiniService({
        serviceId: "2019072365974237", // 插件id,固定值勿改
        servicePage: "pages/hz-enjoy/main/index", // 插件⻚面地址,固定值勿改
        extraData: {
          "alipay.huabei.hz-enjoy.templateId": "2019082600020903160000060462",
          "alipay.huabei.hz-enjoy.partnerId": "2088421609886161",
          "alipay.huabei.hz-enjoy.userId": g.data.list.user_id,
        },
        success: (res) => {
          console.log(JSON.stringify(res));
          // my.alert({
          //   title: JSON.stringify(res) + "1"
          // });
        },
        fail: (res) => {
          console.log(JSON.stringify(res));

          // my.alert({
          //   title: JSON.stringify(res) + '2'
          // });
        },
        complete: (res) => {
          console.log(JSON.stringify(res));
          // my.alert({
          //   title: JSON.stringify(res) + "3"
          // });
        },
      });
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 芝麻信用组件是否可以
    // console.log(my.canIUse('zm-credit-assessment'));
    var that = this;
    // 缓存获取路径
    var imgUrl = wx2my.getStorageSync(['imgUrl1']).data;
    console.log(imgUrl);
    that.setData({
      imgUrl,
    })
    my.setStorageSync({
      key: 'day1',
      data: Moment(new Date()).format('YYYY-MM-DD')
    });
    my.setStorageSync({
      key: 'day2',
      data: Moment(new Date()).add(1, 'day').format('YYYY-MM-DD')
    });

    that.getDatas();
     wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'GetSystem',
        m: 'zh_jdgjb',
      },
      success: (e) => {
        console.log('GetSystem请求成功')
        console.log(e)
        console.log(e);
        wx2my.setStorageSync("platform", e.data)
        wx2my.setNavigationBarColor({
          frontColor: '#000000',
          backgroundColor: '#ffffff'
        })
        wx2my.setNavigationBarTitle({
          title: e.data.pt_name
        })
        that.setData({
          platform: e.data,
          color: e.data.color,
          grade: e.data.open_member
        });
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },

  getDatas() {
    var that = this;
    wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    }); 
    // 精彩生活
    wx2my.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=StrategyCategory&m=zh_jdgjb",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        console.log(res);
        that.setData({
          strategy: res.data.data.list
        });
      }
    });
    // 精彩生活广告位
    wx2my.newRequest({
      url: base.siteroot, // 目标服务器url
      data: {
        from: 'wxapp',
        c: 'entry',
        a: 'wxapp',
        do: 'getad',
        m: 'zh_jdgjb',
        type: 4
      },
      success: (res) => {
        that.setData({
          adImg: res.data[0].logo
        })
      }, fail: (err) => {
        console.log(err)
      }
    }); // 攻略
    // 热门城市
    wx2my.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=HotCity&m=zh_jdgjb",
      header: {
        'content-type': 'application/json' // 默认值

      },
      success: function(res) {
        // console.log(res);
        that.setData({
          hotCity: res.data.data.list
        });
      }
    }); 
    // 精选酒店
    wx2my.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=SelectHotel&m=zh_jdgjb",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        // console.log(res);
        that.setData({
          selectList: res.data.data.list
        });
      }
    }); 
    
    
  },

  content: function() {
    // 查找酒店
    var e = this.data.platform;
    wx2my.navigateTo({
      url: "../hotel_list/hotel_list?nearby=0"
    });
  },
  // 跳转时间选择页
  data_time: function() {
    wx2my.navigateTo({
      // url: './../date_time/index'
      url: './../time/time'
    });
  },
  strategyTo: function(e) {
    // 攻略跳转
    wx2my.navigateTo({
      url: './../article/art_list?title=' + e.currentTarget.dataset.title + '&id=' + e.currentTarget.dataset.id
    });
  },
  search: function() {
    // 搜索附近酒店
    wx2my.navigateTo({
      url: "search",
      success: function(t) { },
      fail: function(t) { },
      complete: function(t) { }
    });
  },
  wode: function() {
    wx2my.reLaunch({
      url: "../logs/logs"
    });
  },
  detail: function(e) {
    // 酒店详情
    console.log(e.currentTarget.dataset.id);
    var id = e.currentTarget.dataset.id;
    wx2my.navigateTo({
      url: "./../hotel_list/hotel_info?hotel_id=" + id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() { },

  tagChoose: function(e) {
    var that = this;
    // console.log(e);
    wx2my.navigateTo({
      url: 'search?address=' + e.currentTarget.dataset.address
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 保存缓存中的数据,使用wx2my封装的读取缓存中数据的方法
    this.setData({
      day1: wx2my.getStorageSync(['day1']).data,
      day2: wx2my.getStorageSync(['day2']).data,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() { }
});